<?php
class ValidController extends AdminAppController{
	
    function valid_category_code()
    {
        $this->autoRender = false;
        $valid = true;
        $category_id = isset($_POST['category_id']) ? $_POST['category_id'] : "";
        $category_code = $_POST['category_code'];
        $this->loadModel('Admin.Category');
        $valid = $this->Category->valid_category_code($category_id, $category_code);
 
        echo json_encode(array(
            'valid' => $valid,
        ));
        
    }
    function valid_link()
    {
        $this->autoRender = false;
        $valid = true;
        $lang_code = $_POST['lang_code'];
        $link = $_POST['link_'.$lang_code];
        $id = isset($_POST['id']) ? $_POST['id'] : '';

        $this->loadModel('Admin.Link');
        $sql = array('conditions'=>array('link =' => $link, 'lang_code =' => $lang_code), 'limit' => '1');
        $data = $this->Link->find('all', $sql);
        if(count($data) > 0)
        {
            if($data[0]['Link']['post_id'] != $id)
                $valid = false;
            else
                $valid = true;
        }
        else
        {
            $valid = true;
        }
            
                
        echo json_encode(array(
            'valid' => $valid,
        ));
        
    }
    function valid_menu_code()
    {
        $this->autoRender = false;
        $valid = true;
        $menu_id = isset($_POST['menu_id']) ? $_POST['menu_id'] : "";
        $menu_code = $_POST['menu_code'];
        $this->loadModel('Admin.Menu');
        $valid = $this->Menu->valid_menu_code($menu_id, $menu_code);
 
        echo json_encode(array(
            'valid' => $valid,
        ));
        
    }
}